package ship.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ship.service.shipservice;

/*1 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
2     req.setCharacterEncoding("utf-8");
3     resp.setContentType("text/html;charset=utf-8");
4     //创建session
5     HttpSession session=req.getSession();
6     session.setAttribute("name", "周博");
7 }

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
 {
    req.setCharacterEncoding("utf-8");
    resp.setContentType("text/html;charset=utf-8");
    HttpSession session=req.getSession();
    PrintWriter out=resp.getWriter();
        out.println(session.getAttribute("name"));
}*/
/**
 * Servlet implementation class logig1
 */
@WebServlet("/loginbyuserServlet")
public class loginbyuserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public loginbyuserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		int id = Integer.parseInt(request.getParameter("sid"));
        String name = request.getParameter("sname");
        //调用业务方法判断用户是否登录成功
        shipservice service = new shipservice();
        boolean result = service.Loginbyusertablesidandsname(id, name);
        if(result){     
        	HttpSession session = request.getSession();
     	    session.setAttribute("sid", id);
            session.setAttribute("sname", name);
            session.setMaxInactiveInterval(60*60);
			/* out.println("登录成功!!" ); */ 
			/*
			 * try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO
			 * Auto-generated catch block e.printStackTrace(); }
			 */
			request.getRequestDispatcher("index2/index2_1_1.jsp").forward(request, response);
        }
        else{
            String mess = "您的代号编码或您的姓名有误,请重新尝试!";
            request.setAttribute("mess", mess); 
            request.getRequestDispatcher("loginbyuser.jsp").forward(request, response);
        }
	}

}
